#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
    int dfs(TreeNode *root, int init)
    {
        if (root == nullptr)
            return 0;
        if (root->left == nullptr && root->right == nullptr)
            return init * 10 + root->val;
        int lsum = dfs(root->left, init * 10 + root->val), rsum = dfs(root->right, init * 10 + root->val);
        return lsum + rsum;
    }
    int sumNumbers(TreeNode *root)
    {
        return dfs(root, 0);
    }
};